﻿using DevelopTool.Bll.RemoteBlls;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Model.RemoteModels;
using Sunny.UI;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace DevelopTool.Forms.RemoteForms
{
    /// <summary>
    /// RDP远程窗体
    /// </summary>
    public partial class RdpForm : BaseUIPage
    {
        /// <summary>
        /// 远程连接对象
        /// </summary>
        private Remote remote;

        private RemoteBll remoteBll;


        public RdpForm(Remote remote)
        {
            InitializeComponent();

            this.remote = remote;
            this.remoteBll = new RemoteBll();
        }

        private void RdpForm_Load(object sender, EventArgs e)
        {

            if (remote.RdpConfigs == null || remote.RdpConfigs.Count <= 0)
            {
                remote.RdpConfigs = remoteBll.DefaultRdpConfigs.Select(it => new RdpConfig { Key = it.Item1, Value = it.Item2 }).Reverse().ToList();
            }

            var sectionGroup = remoteBll.DefaultRdpConfigs.GroupBy(it => it.Item4).Select(it => it.Key).Reverse();

            foreach (string section in sectionGroup)
            {
                //分类容器
                UITitlePanel titlePanel = new UITitlePanel()
                {
                    Height = 40,
                    Dock = DockStyle.Top,
                    Text = section,
                    TitleHeight = 15,
                    StyleCustomMode = false,
                    Style = this.Style,
                    Font = new Font("宋体", 10F)
                };

                this.panelConent.Controls.Add(titlePanel);

                foreach (RdpConfig rdpConfig in remote.RdpConfigs.Where(it => remoteBll.DefaultRdpConfigs.FindAll(x => x.Item4 == section).Select(y => y.Item1).Contains(it.Key)))
                {
                    Panel panel = new Panel()
                    {
                        Dock = DockStyle.Top,
                        Height = 60,
                    };
                    titlePanel.Height += panel.Height;

                    //描述
                    UIRichTextBox richTextBox = new UIRichTextBox()
                    {
                        Dock = DockStyle.Fill,
                        Text = remoteBll.DefaultRdpConfigs.FirstOrDefault(it => it.Item1 == rdpConfig.Key).Item5,
                        TextAlignment = ContentAlignment.MiddleLeft,
                        Font = new Font("宋体", 10F),
                        ReadOnly = true,
                    };
                    panel.Controls.Add(richTextBox);

                    //值
                    UITextBox textBox = new UITextBox()
                    {
                        Dock = DockStyle.Left,
                        Text = rdpConfig.Value,
                        Width = 250,
                        TextAlignment = ContentAlignment.MiddleCenter,
                        Font = new Font("宋体", 10F),
                        TagString = rdpConfig.Key,
                        Tag = "textBoxValue"
                    };
                    panel.Controls.Add(textBox);

                    titlePanel.Controls.Add(panel);
                }

            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            this.ShowSuccessNotifier("保存成功");
        }

        private void Save()
        {
            var textBoxList = this.panelConent.GetControls<UITextBox>(true).FindAll(it => it.Tag.ToString() == "textBoxValue");

            this.remote.RdpConfigs = textBoxList.Select(it => new RdpConfig
            {
                Key = it.TagString,
                Value = it.Text,
            }).ToList();

            this.remote = remoteBll.Save(this.remote);
        }


        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            Save();
            remoteBll.ConnectRdp(this.remote);
        }


        /// <summary>
        /// 另存为文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnSaveAsToFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "远程连接|*.rdp";
            saveFileDialog.FileName = this.remote.NodeName;
            if (saveFileDialog.ShowDialog() != DialogResult.OK) return;

            remoteBll.SaveAsToFile(this.remote,saveFileDialog.FileName);

            this.ShowSuccessNotifier("rdp文件已保存到:" + saveFileDialog.FileName);
        }

    }
}
